Answer:

A correct version of the program is:

' Calculate Miles per Gallon (Correct Version)
'
' second odometer reading is 45,678.3
' first  odometer reading is 45,149.6
' gallons of gas used is 12.5
'
LET MILES = (45678.3 - 45149.6)
PRINT MILES / 12.5
END

The buggy program uses two names: MILES and MILE where there should only be one. Either name is a correct variable name, but you have to use just one name for the one variable.

When Memory is Found for Variables

The first time a variable is used in a program, the QBasic system finds memory for it. This is true for any statement, not just the LET statement. If there is no other information, the system will put a zero into a number variable. Here is a buggy program of the previous question:

' Calculate Miles per Gallon (Buggy Version)
'
LET MILES = (45678.3 - 45149.6)
PRINT MILE / 12.5
END

The name MILE in the PRINT statement is the first time that variable name is seen. (MILES is a completely different name as far as QBasic is concerned.) So the system finds memory for the new variable MILE, and, lacking any other information, puts a zero into it. Now there are two variables in memory:

MILES
528.7
MILE
0

The arithmetic expression MILE / 12.5 will get the 0 from MILE and divide it by 12.5, resulting in 0. Finally the PRINT will write to the monitor:

0

This is a bug, and hard to track down unless you carefully look at the spelling of each variable name in the program. If a program you write mysteriously calculates an incorrect answer of zero, check the spelling of each variable!

QUESTION 11:

What do you think the following program will write to the monitor?

' Calculate Miles per Gallon (Buggy? or not?)
'
LET MILE = (45678.3 - 45149.6)
PRINT MILE# / 12.5
END